package com.itboye.mengwu.app.ui.roof;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.main.GoodsDetailActivity;
import com.itboye.mengwu.app.ui.main.GoodsListActivity;
import com.itboye.mengwu.app.ui.web.WebActivity;
import com.itboye.mengwu.data.model.HomePageModel;
import com.itboye.mengwu.data.model.HomePageModel.Ban2;
import com.itboye.mengwu.data.model.IndexModel.Pos1;
import com.itboye.mengwu.data.model.LunBoModel;
import com.itboye.mengwu.data.model.ProductTypeBean;
import com.itboye.mengwu.data.model.TestModel;
import com.itboye.mengwu.data.presenter.LunBoPresenter;
import com.itboye.mengwu.luntan.comment.view.MyListView;
import com.itboye.mengwu.main.MatchBuyActivity;
import com.itboye.mengwu.widget.CarouselView;
import com.itboye.mengwu.widget.MyGridView;
import com.itboye.mengwu.widget.XScrollView;

public class TodaySpecialOfferItemFragment extends BaseFragment implements
		Observer, OnClickListener {

	CarouselView topLunbo;
	public XScrollView xScrollView;
	private List<ImageView> bmps;
	private List<TestModel> cityList;
	private GridView gvVideoPlayBack;
	private MyGridView gridview_top;
	private List<Pos1> arrayList;
	private List<Ban2> ban2;
	List<com.itboye.mengwu.data.model.HomePageModel.Hot> hotList = new ArrayList<HomePageModel.Hot>();
	LunBoPresenter lunBoPresenter;
	ImageView ad_one;
	TextView txt_reloading;
	LinearLayout linBaoShuiQu;
	LinearLayout matchBuyLayout;

	private LinearLayout onlinLintou;
	MyListView hotgoods;
	ImageView img1, img2, img3, img4, img5;
	ImageView hot1, hot2, hot3, hot4;
	TextView tv1, tv2, tv3, tv4, tv5, tv6;

	// private int[] colors = new int[] { Color.parseColor("#FBE2E6"),
	// Color.parseColor("#D3F1FB"), Color.parseColor("#FBE2E6"),
	// Color.parseColor("#FFEABF"), Color.parseColor("#FBE2E6"),
	// Color.parseColor("#FFEABF"), Color.parseColor("#FFEABF"),
	// Color.parseColor("#FFEABF"), Color.parseColor("#FFEABF"),
	// Color.parseColor("#FFEABF"), Color.parseColor("#FFEABF"),
	// Color.parseColor("#FFEABF") };

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_todayspecialoffer;
	}

	@Override
	public void onMyActivityCreated() {
		xScrollView.smoothScrollTo(0, 0);
		lunBoPresenter = new LunBoPresenter(this);
		LunBoModel model = new LunBoModel();
		//model.setPosition(6045);
		lunBoPresenter.getInfo(model);
		setOnitemLCickListener();
		matchBuyLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent jumpIntennt = new Intent(getActivity(),
						MatchBuyActivity.class);
				startActivity(jumpIntennt);
			}
		});

	}

	private void setOnitemLCickListener() {
		// TODO Auto-generated method stub
		gridview_top.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), GoodsListActivity.class);
				startActivity(intent);
			}
		});
		hotgoods.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						GoodsDetailActivity.class);
				// Log.d("hu", hotList.size() + "arrayList.get(arg2)");
				Log.d("hu", hotList.get(arg2).getPro_list().get(position)
						.getId()
						+ "---pid" + "arg2--" + arg2);
				intent.putExtra("pid",
						hotList.get(arg2).getPro_list().get(position).getId());
				startActivity(intent);

			}
		});

		linBaoShuiQu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						ActivityBaoShuiQu.class);
				intent.putExtra("special", 1);
				startActivity(intent);
			}
		});
		onlinLintou.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), WebActivity.class);
				intent.putExtra("title", "特卖汇");
				startActivity(intent);
			}
		});
	}

	public class MyListViewAdapter extends BaseAdapter {
		List<com.itboye.mengwu.data.model.HomePageModel.Hot> arrayList;

		public MyListViewAdapter(
				List<com.itboye.mengwu.data.model.HomePageModel.Hot> list) {
			// TODO Auto-generated constructor stub
			this.arrayList = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return arrayList.size();

		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return arrayList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			Holder holder = null;
			if (convertView == null) {
				holder = new Holder();
				LayoutInflater inflater = LayoutInflater.from(getActivity());
				convertView = inflater
						.inflate(R.layout.item_roof_hotsale, null);
				holder.hot1 = (ImageView) convertView.findViewById(R.id.hot1);
				holder.hot2 = (ImageView) convertView.findViewById(R.id.hot2);
				holder.hot3 = (ImageView) convertView.findViewById(R.id.hot3);
				holder.hot4 = (ImageView) convertView.findViewById(R.id.hot4);
				holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
				holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
				holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
				holder.tv4 = (TextView) convertView.findViewById(R.id.tv4);
				holder.tv5 = (TextView) convertView.findViewById(R.id.tv5);
				holder.tv6 = (TextView) convertView.findViewById(R.id.tv6);
				TextView[] tvLis = new TextView[arrayList.get(position)
						.getPro_list().size()];
				holder.tv1.setText(arrayList.get(position).getPro_list().get(0)
						.getName());
				holder.tv2.setText(arrayList.get(position).getPro_list().get(1)
						.getName());
				holder.tv3.setText(arrayList.get(position).getPro_list().get(2)
						.getName());
				holder.tv4.setText(arrayList.get(position).getPro_list().get(0)
						.getPrice());
				holder.tv5.setText(arrayList.get(position).getPro_list().get(1)
						.getPrice());
				holder.tv6.setText(arrayList.get(position).getPro_list().get(2)
						.getPrice());

				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ arrayList.get(position).getImg(), holder.hot1);
				XImageLoader.load(
						GlobalConfig.API_URL
								+ "/picture/index?id="
								+ arrayList.get(position).getPro_list().get(0)
										.getImg(), holder.hot2);
				XImageLoader.load(
						GlobalConfig.API_URL
								+ "/picture/index?id="
								+ arrayList.get(position).getPro_list().get(1)
										.getImg(), holder.hot3);
				XImageLoader.load(
						GlobalConfig.API_URL
								+ "/picture/index?id="
								+ arrayList.get(position).getPro_list().get(2)
										.getImg(), holder.hot4);

			}
			return convertView;
		}

		public class Holder {
			ImageView hot1, hot2, hot3, hot4;
			TextView tv1, tv2, tv3, tv4, tv5, tv6;
		}
	}

	public class GridViewTopAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 6;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return arrayList == null ? new ArrayList<ProductTypeBean>()
					: arrayList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MyHolder holder = null;
			if (convertView == null) {
				holder = new MyHolder();
				LayoutInflater layoutInflater = LayoutInflater
						.from(getActivity());
				convertView = layoutInflater.inflate(
						R.layout.homepage_gridview_top_item, null);
				holder.txt_title = (TextView) convertView
						.findViewById(R.id.tv_title);
				holder.img = (ImageView) convertView
						.findViewById(R.id.ItemImage);
				convertView.setTag(holder);
			} else {
				holder = (MyHolder) convertView.getTag();
			}
			holder.txt_title.setText(arrayList.get(position).getTitle());
			holder.txt_title.setVisibility(View.GONE);
			XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
					+ arrayList.get(position).getImg(), holder.img);
			return convertView;
		}

		public class MyHolder {
			private TextView txt_title;
			private ImageView img;
		}

	}

	int position;

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity model = super.handlerError(data);

		xScrollView.smoothScrollTo(0, 0);
		if (model != null) {

			if (model.getEventType() == LunBoPresenter.BannerSuccessEvent) {
				final HomePageModel arrayBanner = (HomePageModel) model
						.getData();
				hotList = arrayBanner.getP_hot();
				bmps = new ArrayList<ImageView>();
				for (int i = 0; i < arrayBanner.getBan1().size(); i++) {
					position = i;
					ImageView imageView = new ImageView(getActivity());

					imageView.setScaleType(ScaleType.FIT_XY);
					com.nostra13.universalimageloader.core.ImageLoader
							.getInstance().displayImage(
									GlobalConfig.API_URL
											+ "/picture/index?id="
											+ arrayBanner.getBan1().get(i)
													.getImg(), imageView);
					bmps.add(imageView);
					// imageView.setTag(pictureBean);
					imageView.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// CarouselPictureBean bean = (CarouselPictureBean)
							// v.getTag();
							Intent intent = new Intent(getActivity(),
									WebActivity.class);
							intent.putExtra("url",
									arrayBanner.getBan1().get(position)
											.getUrl());
							intent.putExtra("title",
									arrayBanner.getBan1().get(position)
											.getTitle());
							startActivity(intent);
						}
					});
				}

				topLunbo.setImageBitmaps(bmps);
				txt_reloading.setVisibility(View.GONE);
				arrayList = new ArrayList<Pos1>();
				for (int i = 0; i < arrayBanner.getPos1().size(); i++) {
					Pos1 cate = new Pos1();
					cate.setTitle(arrayBanner.getPos1().get(i).getTitle());
					cate.setImg(arrayBanner.getPos1().get(i).getImg());
					arrayList.add(cate);
				}

				GridViewTopAdapter adapter1 = new GridViewTopAdapter();

				gridview_top.setAdapter(adapter1);
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ arrayBanner.getBanner2().get(0).getImg(), ad_one);

				ad_one.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(getActivity(),
								WebActivity.class);
						intent.putExtra("url", arrayBanner.getPos1().get(0)
								.getUrl());
						intent.putExtra("title", arrayBanner.getPos1().get(0)
								.getTitle());
						startActivity(intent);
					}
				});
				img1.setScaleType(ScaleType.FIT_XY);
				img2.setScaleType(ScaleType.FIT_XY);
				img3.setScaleType(ScaleType.FIT_XY);
				img4.setScaleType(ScaleType.FIT_XY);
				img5.setScaleType(ScaleType.FIT_XY);
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ arrayBanner.getPos2().get(0).getImg(), img1);
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ arrayBanner.getPos2().get(1).getImg(), img2);
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ arrayBanner.getPos2().get(2).getImg(), img3);
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ arrayBanner.getPos2().get(3).getImg(), img4);
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ arrayBanner.getPos2().get(4).getImg(), img5);

				MyListViewAdapter myadapter = new MyListViewAdapter(
						arrayBanner.getP_hot());
				hotgoods.setAdapter(myadapter);
			}

		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.img1:
			Intent intent = new Intent(getActivity(), WebActivity.class);
			startActivity(intent);
			break;
		case R.id.img2:
			Intent intent2 = new Intent(getActivity(), WebActivity.class);
			startActivity(intent2);
			break;
		case R.id.img3:
			Intent intent3 = new Intent(getActivity(), WebActivity.class);
			startActivity(intent3);
			break;
		case R.id.img4:
			Intent intent4 = new Intent(getActivity(), WebActivity.class);
			startActivity(intent4);
			break;
		case R.id.img5:
			Intent intent5 = new Intent(getActivity(), WebActivity.class);
			startActivity(intent5);
			break;

		default:
			break;
		}
	}
}
